<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%
	String filePath = request.getParameter("filePath");
	if(filePath==null){
		filePath="";
	}
 %> 
<!-- 合格证发票上传图片专用 -->
<script type="text/javascript" src="<%=path %>/resource/lib/plugins/ajaxfileupload.js"></script>
<span class="" id="add_upfileBox4"></span>
<div class="upfileBoxs4"></div>
<span class="sq-button sq-button-hasicon" id="btnUpload4"><i class="sqBtnIco_9"></i>上传</span>
<input type="hidden" id="filePath" value="<%=filePath %>"/>
<script type="text/javascript">
  
      var count = 1;
      var upload_result_params = []; 
   	  var serverPrefixPath = "";
      
      $(function(){
    	  $("#add_upfileBox4").click();
    	  
    	  //获取服务器路径前缀
    	  $.ajax({
	    		type: 'get',
	    		url: '<%=path%>/system/sysulfilesrec/getServerPath.do',
	    		success:function(msg){
	    			serverPrefixPath = msg;
	    			console.log(serverPrefixPath);
	    		}
	    	});
      });
      
      $("#btnUpload4").click(function() {
    	   	ajaxFileUploadImg4();
      });
      var FileCount=1;
      $("#add_upfileBox4").click(function(event) {
    	  if(FileCount>10){
    		  $.ligerDialog.SQalert({content: "超出最大上传数量" , SQ_type: "warning" });
    		  return;
    	  }
          $Html=CreatUploadInputBox({
              id:"uploadk"+FileCount,  //id不跟着序号走！
              clearLine: true,
              deletable: false,
              onDelete: function  (upfileBox) {
            	  FileCount--;
                  //setUpfileCount (); //删除的时候重新设置序号
                  var delFileName = upfileBox.find('[type=file]')[0].files[0].name;
                  var temp;
                  for(temp = 0; temp < upload_result_params.length;temp++){
                	  if(upload_result_params[temp].fileNm == delFileName){
                		  upload_result_params.splice(temp,1);
                		  break;
                	  }
                  }
              },
              onChange: function (e) {
                  $(e.target).remove_afterUploadIcon();//删除时,移除已上传文件的下载按钮
              }
          });
          //$Html.prepend('<span class="UpfileCount"></span>');
          $(".upfileBoxs4").append($Html);
          //$Html.insertBefore($(this));
          //$Html.appendTo($("#more"));
          //setUpfileCount (); //新增组件后，设置序号
          FileCount++;
      });
      //function setUpfileCount () { //设置序号
      //    $(".UpfileCount").each(function(index, el) {
      //        $(this).html(index+1);
     //     });
      	  
     // }
      	
        //图片类型验证
	function verificationPicFile(file) {
		    var fileTypes = [".bmp",".jpg",".jpeg",".png",".tif",".gif",".pcx",".tga",".exif",".fpx",".svg",".psd",".cdr",".pcd",".dxf",".ufo",".eps",".ai",".raw",".wmf",".webp"];
		    var filePath = file.value;
		    //当括号里面的值为0、空字符、false 、null 、undefined的时候就相当于false
		    if(filePath){
		        var isNext = false;
		        var fileEnd = filePath.substring(filePath.indexOf("."));
		        for (var i = 0; i < fileTypes.length; i++) {
		            if (fileTypes[i] == fileEnd.toLowerCase()) {
		                isNext = true;
		                break;
		            }
		        }
		        if (!isNext){
		            alert('只能上传图片');
		            file.value = "";
		            return false;
		        }
		    }else {
		        return false;
		    }
		  return true;
    }
        function ajaxFileUploadImg4(){  
            //获取file的全部id  
            var uplist = $("input[id^=uploadk]"); 
            //return;
            if(uplist.length==0){
            	$.ligerDialog.SQalert({content: "没有要上传的内容！" , SQ_type: "warning" });
            	return;
            }
	        var arrId = [];
	        var fileNames = [];//用于判断是否有重名文件
	        for (var i=0; i< uplist.length; i++){  
	        	if(uplist[i].value){  
	                arrId[i] = uplist[i].id; 
	                if($.inArray(uplist[i].value, fileNames) >= 0){
	                	$.ligerDialog.SQalert({content: uplist[i].value+"文件重名！" , SQ_type: "warning" });
	                	return;
	                }
	                fileNames[i] = uplist[i].value;
	              //格式必须是图片
		            if(!verificationPicFile(uplist[i]))
		            	return;
	            }
	            
            }
	        var _filePath = $("#filePath").val();
       		var wait_dialog=$.ligerDialog.SQalert({content: '上传中,请稍后...' ,SQ_type: "waitting" });
	        $.ajaxFileUpload({  
	            url:'<%=path%>/system/sysulfilesrec/upload.do',
	            secureuri:false,  
	            fileElementId: arrId,  //这里不在是以前的id了，要写成数组的形式！  
	            dataType: 'json',  
	            data: {filePath:_filePath},  
	            success: function (msg){ 
	            	wait_dialog.close();
	            	if(msg.success == 1){
	                    $.ligerDialog.SQalert({content:msg.info, SQ_type: "success"});
		            	//alert(JSON.stringify(msg.data));
	            		upload_result_params=msg.data;
	            		uploadCallbackForMore(msg.data,4);
	            		
	            		//遍历msg.data 为filePath加上服务器路径前缀
	            		
	            		if(serverPrefixPath.length >1 && serverPrefixPath.indexOf("\"")>=0){
	            			serverPrefixPath = serverPrefixPath.substring(1,serverPrefixPath.length-1);
	            		}
	            		var uploadJOSN = JSON.parse(JSON.stringify(msg.data));;
	            		for (var i=0; i< uploadJOSN.length; i++){  
	            			var oldPath = "";
	            			var newPath = "";
	            			oldPath = uploadJOSN[i].filePath;
	            			newPath = serverPrefixPath + oldPath;
	            			uploadJOSN[i].filePath = newPath;
	            		}
	            		
	            		//上传成功后在文件后加入下载按钮
	            		$('.upfileBoxs4 [type=file]').each(function(index, el) {
	                        $(el).afterUploadFile({
	                            filenameField: 'fileNm', //默认值就是fileNm
	                            filePathField: 'filePath', //默认值就是filePath
	                            files_DATA: uploadJOSN
	                        });
	                    });
	                } else{
	                    $.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
	                }
	            },
	            complete: function(){
	            	
	            }
	        });  
    	}  
        //获取上传结果参数
        function getUploadResultParams(){
        	return upload_result_params;
        }
        //清除上传结果参数
        function clearUploadResultParams(){
        	//移除文件上传标签
        	$(".fileUploadBox").remove();
        	upload_result_params = [];//设置参数为空
        	FileCount = 1;
        	$("#add_upfileBox4").click();
        }

</script>
